﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMISLibrary.Entities;
using CMISLibrary;
using ObjectDanhMuc.DB;

namespace ObjectDanhMuc
{
    public class cls_NPC_LIBRARY_Controller
    {
         #region   Atributes

	    //private DataSet CMIS_Header = new CMISOutputParameter();
	    private DataSet ds = new DataSet();
	    private DataTable dt = new DataTable();
	    private NPC_LIBRARY info;

	    #endregion

        #region   Constructor

	    public cls_NPC_LIBRARY_Controller()
	    {
		    info = new NPC_LIBRARY();
	    }
        public cls_NPC_LIBRARY_Controller(NPC_LIBRARY Info)
	    {
		    this.info = Info;
	    }

	    #endregion


        #region   Properties

        public NPC_LIBRARY pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion

        #region Method

        public IEnumerable<NPC_LIBRARY> select_NPC_LIBRARY_by_LIBID(long longLIBID)
        {
            try
            {
                var q = from p in CMIS2.DB.NPC_LIBRARY
                        orderby p.LIBID ascending
                        where p.LIBID == longLIBID
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public IEnumerable<NPC_LIBRARY> select_NPC_LIBRARY_1by1()
        {
            try
            {
                CMIS2.ResetDB();

                var q = from p in CMIS2.DB.NPC_LIBRARY
                        orderby p.LIBID ascending
                        where (info.LIBNAME != " " && p.LIBNAME.ToUpper().Contains(info.LIBNAME.ToUpper()) || info.LIBNAME == " ")
                        && (info.ASSEMBLYNAME != " " && p.ASSEMBLYNAME.ToUpper().Contains(info.ASSEMBLYNAME.ToUpper()) || info.ASSEMBLYNAME == " ")
                        select p;
                return q;
            }
            catch
            {
                CMIS2.ResetDB(); return null;
            }
        }

        public bool insert_NPC_LIBRARY()
        {
            try
            {
                NPC_LIBRARY objNPC_LIBRARY = new NPC_LIBRARY()
                {
                    SUBDIVISIONID = info.SUBDIVISIONID, 
                    LIBID = info.LIBID, 
                    LIBNAME = info.LIBNAME, 
                    NAMESPACE = info.NAMESPACE, 
                    ASSEMBLYNAME = info.ASSEMBLYNAME, 
                    PRIORITY = info.PRIORITY, 
                    STATE = info.STATE,
                    CREATEDDATE = info.CREATEDDATE, 
                    CREATEDBY = info.CREATEDBY
                };
                CMIS2.DB.NPC_LIBRARY.InsertOnSubmit(objNPC_LIBRARY);
                CMIS2.DB.SubmitChanges();
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                CMIS2.ResetDB(); return false;
            }
        }

        public bool update_NPC_LIBRARY()
        {
            try
            {
                NPC_LIBRARY objNPC_LIBRARY = CMIS2.DB.NPC_LIBRARY.Single(c => c.LIBID == info.LIBID);
                objNPC_LIBRARY.SUBDIVISIONID = info.SUBDIVISIONID;
                objNPC_LIBRARY.LIBNAME = info.LIBNAME;
                objNPC_LIBRARY.NAMESPACE = info.NAMESPACE;
                objNPC_LIBRARY.ASSEMBLYNAME = info.ASSEMBLYNAME;
                objNPC_LIBRARY.PRIORITY = info.PRIORITY;
                objNPC_LIBRARY.STATE = info.STATE;
                objNPC_LIBRARY.CREATEDDATE = info.CREATEDDATE;
                objNPC_LIBRARY.CREATEDBY = info.CREATEDBY;

                CMIS2.DB.SubmitChanges();

                return true;
            }
            catch
            {
                CMIS2.ResetDB(); return false;
            }
        }

        public bool delete_NPC_LIBRARY(ref string strErr)
        {
            try
            {
                var query = from p in CMIS2.DB.NPC_LIBRARY where p.LIBID == info.LIBID select p;
                if (query != null && query.Count() > 0)
                {
                    CMIS2.DB.NPC_LIBRARY.DeleteAllOnSubmit(query.ToList());
                    CMIS2.DB.SubmitChanges();
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                CMIS2.ResetDB(); return false;
            }
        }

        #endregion

        #region SEQ

        public long GetNextSequence(ref string strErr)
        {

            try
            {

                //string strErr = "";
                long _ID = CMIS2.DB.ExecuteCommand("select seq_NPC_LIBRARY.nextval from dual", ref strErr);

                if (strErr == "") return _ID;

                else return -1;

            }

            catch
            {

                CMIS2.ResetDB(); return -1;
            }

        }
        
       

        #endregion 
    }
}
